import Vue from'vue'
export default {
	state:{
		cartList:[
			{
					"count":1,
					"selected":false,
			    "promId": 0,
			    "showPoints": false,
			    "itemTagList": [
			        {
			            "itemId": 3532007,
			            "tagId": 128111155,
			            "freshmanExclusive": false,
			            "name": "暖冬特惠",
			            "subType": 204,
			            "forbidJump": false,
			            "type": 2
			        }
			    ],
			    "rank": 1,
			    "id": 3532007,
			    "sellVolume": 2532,
			    "primaryPicUrl": "https://yanxuan-item.nosdn.127.net/54e9c325ef69dfead72bdb6859feb2f3.png",
			    "soldOut": false,
			    "sortFlag": 0,
			    "commentCount": 0,
			    "onSaleTime": 1569859202422,
			    "picMode": 1,
			    "commentWithPicCount": 0,
			    "underShelf": false,
			    "status": 2,
			    "couponConflict": true,
			    "forbiddenBuy": false,
			    "promotionDesc": "暖冬特惠",
			    "limitedFlag": 204,
			    "pieceNum": 0,
			    "itemSizeTableDetailFlag": false,
			    "forbidExclusiveCal": false,
			    "rewardShareFlag": false,
			    "updateTime": 1576741386256,
			    "showCommentEntrance": true,
			    "pieceUnitDesc": "件",
			    "specialPromTag": "",
			    "counterPrice": 1099,
			    "categoryL2Id": 0,
			    "retailPrice": 802,
			    "primarySkuPreSellPrice": 0,
			    "preLimitFlag": 0,
			    "itemPromValid": true,
			    "promTag": "暖冬特惠",
			    "source": 0,
			    "points": 0,
			    "primarySkuPreSellStatus": 0,
			    "extraServiceFlag": 0,
			    "flashPageLink": "",
			    "autoOnsaleTimeLeft": 0,
			    "innerData": {},
			    "saleCenterSkuId": 0,
			    "pointsStatus": 0,
			    "extraPrice": "",
			    "colorNum": 4,
			    "showTime": 0,
			    "autoOnsaleTime": 0,
			    "preemptionStatus": 1,
			    "isPreemption": 0,
			    "zcSearchFlag": false,
			    "name": "极地探险都不怕，女式地表强温鹅绒羽绒服",
			    "appExclusiveFlag": false,
			    "itemType": 1,
			    "listPicUrl": "https://yanxuan-item.nosdn.127.net/de6493e42fe69d483df949871585c13e.png",
			    "pointsPrice": 0,
			    "simpleDesc": "90%白鹅绒，充沛保暖之选",
			    "seoTitle": "",
			    "newItemFlag": false,
			    "buttonType": 0,
			    "primarySkuId": 300004186,
			    "displaySkuId": 300004193,
			    "productPlace": "",
			    "itemSizeTableFlag": false
			},
			{
					"count":2,
					"selected":true,
			    "promId": 0,
			    "showPoints": false,
			    "itemTagList": [
			        {
			            "itemId": 3533004,
			            "tagId": 128111155,
			            "freshmanExclusive": false,
			            "name": "暖冬特惠",
			            "subType": 204,
			            "forbidJump": false,
			            "type": 2
			        }
			    ],
			    "rank": 1,
			    "id": 3533004,
			    "sellVolume": 1859,
			    "primaryPicUrl": "https://yanxuan-item.nosdn.127.net/89c86d80100a5b876a898955c09cd047.png",
			    "soldOut": false,
			    "sortFlag": 0,
			    "commentCount": 0,
			    "onSaleTime": 1569859202763,
			    "picMode": 1,
			    "commentWithPicCount": 0,
			    "underShelf": false,
			    "status": 2,
			    "couponConflict": true,
			    "forbiddenBuy": false,
			    "promotionDesc": "暖冬特惠",
			    "limitedFlag": 204,
			    "pieceNum": 0,
			    "itemSizeTableDetailFlag": false,
			    "forbidExclusiveCal": false,
			    "rewardShareFlag": false,
			    "updateTime": 1576741589742,
			    "showCommentEntrance": true,
			    "pieceUnitDesc": "件",
			    "specialPromTag": "",
			    "counterPrice": 1299,
			    "categoryL2Id": 0,
			    "retailPrice": 896,
			    "primarySkuPreSellPrice": 0,
			    "preLimitFlag": 0,
			    "itemPromValid": true,
			    "promTag": "暖冬特惠",
			    "source": 0,
			    "points": 0,
			    "primarySkuPreSellStatus": 0,
			    "extraServiceFlag": 0,
			    "flashPageLink": "",
			    "autoOnsaleTimeLeft": 0,
			    "innerData": {},
			    "saleCenterSkuId": 0,
			    "pointsStatus": 0,
			    "extraPrice": "",
			    "colorNum": 3,
			    "showTime": 0,
			    "autoOnsaleTime": 0,
			    "preemptionStatus": 1,
			    "isPreemption": 0,
			    "zcSearchFlag": false,
			    "name": "穿上冬日小火炉，女式地表强温宽松羽绒服",
			    "appExclusiveFlag": false,
			    "itemType": 1,
			    "listPicUrl": "https://yanxuan-item.nosdn.127.net/cd41af69033066f251c9fbdcb730c517.png",
			    "pointsPrice": 0,
			    "simpleDesc": "90%白鹅绒，温暖有保障",
			    "seoTitle": "",
			    "newItemFlag": false,
			    "buttonType": 0,
			    "primarySkuId": 300004169,
			    "displaySkuId": 300004207,
			    "productPlace": "",
			    "itemSizeTableFlag": false
			}			
		]
	},
		
	mutations:{
		//修改商品选中状态
		changeSelected(state,{selected,index}){
			state.cartList[index].selected = selected
		},
		//修改商品数量
		changeCount(state,{type,index}){
			//判断type
			if(type){
				//增加1
				state.cartList[index].count +=1
			}else{
				//减少1
				if(state.cartList[index].count>1){
					state.cartList[index].count -=1
				}else{
					//如果只有一个商品了，在减少商品数量那么就移除
					state.cartList.splice(index,1)
				}
			}
		},
		//全选和全不选
		handlerAllSelected(state,selected){
			state.cartList.forEach(item=>item.selected = selected)
		},
		//修改购物车中商品的信息
		changeCartList(state,shopItem){
			//输出商品信息
			console.log(shopItem)
			//购物车中如果有这个商品，就是修改数量，如果没有，那么就添加一个，数量默认就是1，
			const shopItem1 = state.cartList.find (item=>item.id===shopItem.id)
			//判断   说明商品存在
			if(shopItem1){
				shopItem1.count +=1
			}else{
				//不存在，里面没有count和selected
				//向购物车数组中添加对象，保证数据是响应式的，里面的属性也是响应式的
				Vue.set(shopItem,'count',1)
				Vue.set(shopItem,'selected',true)
				//把对象添加到数组中
				state.cartList.push(shopItem)
			}
		}
	},
	action:{
	},
	getters:{
		//总数量
		totalCount(state){
			return state.cartList.reduce((pre,item)=>{
				if(item.selected){
					pre +=item.count
				}
				return pre
			},0)
		},
		//总价格
		totalPrice(state){
			return state.cartList.reduce((pre,item)=>{
				if(item.selected){
					pre +=item.count * item.retailPrice
				}
				return pre
			},0)
		},
		//全选全不选
		isAllChecked(state){
			return state.cartList.every(item=>item.selected)
		}
	}
}